/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSElement.cpp
 *
 *  Created on: 2010/02/16
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSElement.h"
#include <QMetaObject>
#include <QMetaProperty>
#include <QList>
#include <QXmlStreamWriter>

namespace Realsphere {

    RSElement::RSElement ( const QString& rTag ) :
        RSObject ( 0 )
    {
        mTag = rTag;
        mRootElement = this;
        mParentElement = NULL;
    }

    RSElement::~RSElement ()
    {
        mChildElements.clear ();
        mRootElement = NULL;
        mParentElement = NULL;
    }

    RSElement* RSElement::RootElement () const
    {
        return mRootElement;
    }

    RSElement* RSElement::ParentElement () const
    {
        return mParentElement;
    }

    RSElement* RSElement::GetChildElement ( unsigned int rIndex ) const
    {
        return mChildElements[rIndex];
    }

    int RSElement::IndexOf ( RSElement* rElement )
    {
        return mChildElements.indexOf ( rElement );
    }

    unsigned int RSElement::ChildElementsSize ()
    {
        return mChildElements.size ();
    }

    RSElements RSElement::ChildElements () const
    {
        return mChildElements;
    }

    RSElementsIterator RSElement::ChildElementsIterator () const
    {
        return RSElementsIterator ( mChildElements );
    }

    void RSElement::WriteXml ( QXmlStreamWriter* rXmlWriter )
    {
        rXmlWriter->writeStartElement ( mTag );
        WriteElementAttributes ( rXmlWriter );
        WriteElementData ( rXmlWriter );
        WriteElementChilds ( rXmlWriter );
        rXmlWriter->writeEndElement ();
    }

    void RSElement::WriteElementAttributes ( QXmlStreamWriter* rXmlWriter )
    {
        int total_properties = metaObject ()->propertyCount ();

        for ( int i = 0; i < total_properties; i++ )
        {
            QMetaProperty property = metaObject ()->property ( i );
            QString name = property.name ();
            QVariant variant = property.read ( this );

            if ( !name.startsWith ( "_attr_" ) )
                continue;

            if ( variant.toString () != "" )
                rXmlWriter->writeAttribute ( name.remove ( "_attr_" ), variant.toString () );
        }
    }

    void RSElement::WriteElementChilds ( QXmlStreamWriter* rXmlWriter )
    {
        for ( int i = 0; i < mChildElements.size (); i++ )
        {
            RSElement* element = mChildElements[i];
            element->WriteXml ( rXmlWriter );
        }
    }

    void RSElement::WriteElementData ( QXmlStreamWriter* rXmlWirter )
    {
    }

    RSError RSElement::BeforeAddChildElement ( RSElement* rChild )
    {
        if ( rChild == NULL )
            return RS_NULL_POINTER;

        if ( rChild->ParentElement () == this )
            return RS_INVALID_CHILD;

        // TODO: Handle Extra and Asset

        return RS_INVALID_ELEMENT;
    }

    RSError RSElement::BeforeRemoveChildElement ( RSElement* rChild )
    {
        if ( rChild == NULL )
            return RS_NULL_POINTER;

        if ( rChild->ParentElement () != this )
            return RS_INVALID_CHILD;

        // TODO: Handle Extra and Asset

        return RS_INVALID_ELEMENT;
    }

    bool RSElement::AfterAddChildElement ( RSElement* rChild )
    {
        // TODO: Handle Extra and Asset
        return false;
    }

    bool RSElement::AfterRemoveChildElement ( RSElement* rChild )
    {
        // TODO: Handle Extra and Asset
        return false;
    }

    void RSElement::SetParentElement ( RSElement* rParentElement )
    {
        mParentElement = rParentElement;
    }

    RSError RSElement::AddChildElement ( RSElement* rChild )
    {
        RSError error = BeforeAddChildElement ( rChild );

        if ( !error )
        {
            if ( rChild->ParentElement () != NULL )
                rChild->ParentElement ()->RemoveChildElement ( rChild );

            rChild->SetParent ( this );
            rChild->SetParentElement ( this );
            rChild->SetRootElement ( mRootElement, false );

            mChildElements.append ( rChild );

            AfterAddChildElement ( rChild );
            emit ChildElementAdded ( this, rChild );
        }

        return error;
    }

    RSError RSElement::RemoveChildElement ( RSElement* rChild )
    {
        RSError error = BeforeRemoveChildElement ( rChild );

        if ( error )
            return error;

        int index = mChildElements.indexOf ( rChild );

        if ( index < -1 )
            return RS_OBJECT_NOT_FOUND;

        rChild->setParent ( NULL );
        rChild->SetRootElement ( NULL, false );
        mChildElements.removeAt ( index );

        AfterRemoveChildElement ( rChild );
        emit ChildElementRemoved ( this, rChild );

        return RS_NO_ERROR;
    }

    RSError RSElement::RemoveChildElement ( unsigned int rIndex )
    {
        RSElement* element = mChildElements[rIndex];
        return RemoveChildElement ( element );
    }

    RSError RSElement::SetRootElement ( RSElement* rRootElement, bool rNotify )
    {
        if ( rRootElement == this )
        {
            mRootElement = this;
            return RS_NO_ERROR;
        }

        if ( IsRootElement () )
            return RS_ELEMENT_ROOT;

        if ( rRootElement != NULL )
            if ( !rRootElement->IsRootElement () )
                return RS_ELEMENT_NOT_ROOT;

        mRootElement = rRootElement;

        RSElementsIterator iterator = ChildElementsIterator ();

        while ( iterator.hasNext () )
        {
            RSElement* element = iterator.next ();
            element->SetRootElement ( rRootElement, rNotify );
        }

        if ( rNotify )
            emit RootElementChanged ( this, rRootElement );

        return RS_NO_ERROR;
    }

    void RSElement::SetTag ( const QString& rTag, bool rEmit ) {
        mTag = rTag;

        if ( rEmit )
            emit TagNameChanged ( this, mTag );
    }

    QString RSElement::Tag () const {
        return mTag;
    }

    RSElement* RSElement::operator [] ( int rIndex )
    {
        return mChildElements[rIndex];
    }
}
